import { PageContainer } from '@ant-design/pro-layout';
import { Table,Card,Button,Modal, Input,Popover, InputNumber,Radio,Alert } from 'antd';
import {SearchOutlined,PlusOutlined} from '@ant-design/icons'
import {UseSearch,UseTable} from '@/components'
import React from 'react';
import moment from 'moment';
import OfferedController from '@/services/OfferedProject/OfferedController';
import { getStr } from '@/components/function';
import Detail from '../detail'


const {confirm} = Modal
class ProjectList extends React.Component {
    constructor(props) {
        super(props);
        let userInfo = JSON.parse(sessionStorage.getItem("userInfo"))
        let level = ''
        if(userInfo.roleName.includes("市级")){
            level='市级'
        }
        if(userInfo.targs.includes("省级")){
            level='省级'
        }
        let show = userInfo.targs.includes("办公室")
        this.state = { 
            dataSource:[],
            datas:{},
            freshen:false,
            level:level,
            userInfo:userInfo,
            show:show
         };
    }
    showNewProject=()=>{
        this.setState({
            visibleNewProject:true
        })
    }
    freshen=()=>{
        this.setState({freshen:!this.state.freshen})
    }
    handleSearch=(query)=>{
        this.setState({query})
        this.freshen()

    }
    reset=()=>{
        this.setState({query:{}})
        this.freshen()

    }
    getList=(query)=>{
        let {level} = this.state
        query.level=level
        return OfferedController.getProjectList(query)
    }
    onCancel=()=>{
        this.setState({visibleNewProject:false,visibleResult:false})
        this.freshen()
    }
    toDetail=(rowData)=>{
        this.setState({
            rowData,
            visibleDetail:true
        })
    }
    toResult=(rowData)=>{
        this.setState({
            rowData,
            visibleResult:true
        })
    }
    onRef=(ref)=>{
        this.setState({
            modal:ref
        })
    }
    toReview=(rowData)=>{
       this.setState({
           rowData,
           visibleReview:true
       })
    }
    onRef=(ref)=>{
        this.setState({modal:ref})
    }
    submit=()=>{
        this.state.modal.submit()
    }
    toFiles=(text)=>{
        this.setState({
            visibleFile:true,
            dataSource:JSON.parse(text)
        })
    }
    handleChange=(key,e)=>{
        let {datas} = this.state
        datas[key] = e.target?e.target.value :e
        this.setState({datas})
    }
    reviewSubmit=()=>{
        let {datas,rowData,userInfo} = this.state
        let params = {
            ...datas,
            projectCode:rowData.projectCode,
            userName:userInfo.userName
        }
        let that = this
        confirm({
            title:'提示',
            content:'确定要提交吗？',
            onOk(){
                OfferedController.officeAgreementSubmit(params).then(r=>{
                    if(r&&r.data===1){
                        that.freshen()
                        that.setState({visibleReview:false})
                    }
                })
            }
        })
    }
    tips=()=>{
        return<Alert
        message="权限异常"
        description="您没有权限查看本页面，如有疑问，请联系管理员"
        type="info"
        />
    }
    render() {
        const {rowData,visibleReview,freshen,visibleDetail,query,dataSource,visibleFile,show}=this.state
        const columns=[

            {title:'项目名称',dataIndex:'projectName',align:'center'},
            {title:'所属部门',dataIndex:'department',align:'center',render:text=>text&&text.split("/")[1]},
            {title:'任务描述',dataIndex:'description',align:'center',render:text=>getStr(text)},
            {title:'发布人',dataIndex:'createdBy',align:'center'},
            {title:'发布时间',dataIndex:'createdAt',align:'center'},
            {title:'相关附件',dataIndex:'fileItems',align:'center',render:text=>text&&<a onClick={this.toFiles.bind(this,text)}>查看</a>},
            {title:'计划认领人数',dataIndex:'participantNumber',align:'center'},
            {title:'级别',dataIndex:'level',align:'center'},
            {title:'审核结果',dataIndex:'applyReviewResult',align:'center'},
            {title:'操作',align:'center',render:(text,rowData)=><div>
                {rowData.applyReviewResult==='待审批'&&<Button onClick={this.toReview.bind(this,rowData)} type={'primary'} style={{margin:5}}>审批</Button>}
                </div>},
        ]
        const buttonStyle={margin:5}
        const searchItem=[
            {name:'项目审核结果',keyName:'applyReviewResult',valueType:'select',selectDatas:[{code:'待审批',value:'待审批'},{code:'同意',value:'同意'},{code:'不同意',value:'不同意'}]},
            {name:'项目名称',keyName:'projectName',valueType:'text'},
        ]
        const fileColumns=[
            {title:'序号',dataIndex:'url',render:(text,row,index)=>index+1},
            {title:'文件名',dataIndex:'fileName'},
            {title:'描述',dataIndex:'description',render:text=>getStr(text)},
            {title:'操作',dataIndex:'url',render:(text,row,index)=><a href={text} download={row.fileName}>下载</a>},
        ]
        return (
            <PageContainer
            >
                {show?<div>
                <Card>
                <UseSearch searchItem={searchItem} handleSearch={this.handleSearch} reset={this.reset}/>  
                </Card>
                <Card>
                    <UseTable columns={columns} getData={this.getList} bordered rowKey={'projectCode'} refresh={freshen} query={query}/>
                </Card>
                </div>:this.tips()}
                <Modal
                visible={visibleReview}
                title={'审批'}
                onCancel={()=>this.setState({visibleReview:false})}
                onOk={this.reviewSubmit}
                >
                    <div>
                        <strong>审批结果：</strong>
                        <Radio.Group onChange={this.handleChange.bind(this,'result')} style={{width:350}}>
                            <Radio value={"同意"} key={1}>同意</Radio>
                            <Radio value={"不同意"} key={2}>不同意</Radio>
                        </Radio.Group>
                    </div>
                    <div>
                        <strong>设置积分：</strong>
                        <InputNumber onChange={this.handleChange.bind(this,'score')} style={{width:350}} min={0}/>
                    </div>
                    <div>
                        <strong style={{verticalAlign:"top"}}>审批备注：</strong>
                        <Input.TextArea placeholder="请输入" onChange={this.handleChange.bind(this,'remark')} style={{width:350}}></Input.TextArea>
                    </div>
                </Modal>
                <Modal
                visible={visibleDetail}
                title={'项目详情'}
                onCancel={()=>this.setState({visibleDetail:false})}
                footer={false}
                width={"80%"}
                >
                    {visibleDetail&&<Detail rowData={rowData}/>}
                </Modal>
                {visibleFile&&<Modal
                visible={visibleFile}
                onCancel={()=>this.setState({visibleFile:false})}
                footer={false}
                >
                    <Table dataSource={dataSource} columns={fileColumns} bordered rowKey={'url'} />
                </Modal>}
            </PageContainer>
        );
    }
}

export default ProjectList;